package io.renren.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.entity.ArticalmessageEntity;
import io.renren.entity.Result;
import io.renren.service.ArticalmessageService;
import io.renren.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.UUID;

/**
 * describe:
 * create @ kylin  2019/03/14
 */
@RestController
@RequestMapping("/api/message")
@Api(tags = "文章评论")
public class ApiAritcalMessageController {

    @Autowired
    private ArticalmessageService articalmessageService;

    @ApiOperation(value = "获取到某一篇文章的所有评论信息")
    @GetMapping(value = "/getMessage/{id}")
    public Result<ArticalmessageEntity> getMessage(@PathVariable("id") Integer id) throws Exception{
        String ids =Integer.toString(id);
        QueryWrapper<ArticalmessageEntity> queryWrapper = new QueryWrapper();
        return ResultUtil.success(this.articalmessageService.list(queryWrapper.lambda().eq(ArticalmessageEntity::getToarticalid,ids)));
    }

    @ApiOperation(value = "提交文章的评论信息")
    @PostMapping(value = "save")
    public Result<ArticalmessageEntity> save(@RequestParam("messageauthor")  String messageauthor, @RequestParam("articaltitle")  String articaltitle,
                                             @RequestParam("articalmessage")  String articalmessage, @RequestParam("useravater")  String useravater,
                                             @RequestParam("touser") String touser, @RequestParam("toarticalid") String toarticalid)throws Exception{
        ArticalmessageEntity articalmessageEntity = new ArticalmessageEntity();
        articalmessageEntity.setToarticalid(toarticalid);
        articalmessageEntity.setMessageauthor(messageauthor);
        articalmessageEntity.setArticaltitle(articaltitle);
        articalmessageEntity.setUseravater(useravater);
        articalmessageEntity.setTouser(touser);
        articalmessageEntity.setArticalmessage(articalmessage);
        articalmessageEntity.setCratetime(new Date());
        articalmessageEntity.setUuid(UUID.randomUUID().toString());

        boolean flag = this.articalmessageService.save(articalmessageEntity);
        if (flag){
            return ResultUtil.success("插入成功");
        } else {
            return ResultUtil.success("fail");
        }
    }
}
